<?php
/**
 * The following variables are available in this template:
 * - $this: the CrudCode object
 */
?>
<?php
echo "<?php\n";
$label=$this->pluralize($this->class2name($this->modelClass));
echo "\$this->pageTitle = 'Manage $label';\n";
echo "\$this->breadcrumbs = array(
	'$label'=>array('index'),
	'Manage',
);\n";
?>
  

 
//$this->menu=array(
//	array('label'=>'Create <?php echo $this->modelClass; ?>', 'url'=>array('create')),
//);
//
//Util::registerScript('search', "
//$('.search-button').click(function(){
//	$('.search-form').toggle();
//	return false;
//});
//$('.search-form form').submit(function(){
//	$.fn.yiiGridView.update('<?php echo $this->class2id($this->modelClass); ?>-grid', {
//		data: $(this).serialize()
//	});
//	return false;
//});
//
//");


?>
<div class="heading">
    <h1><img src="<?php echo '<?php echo Util::themeUrl() ?>'; ?>/images/default.png" /> Manage <?php echo $this->pluralize($this->class2name($this->modelClass)); ?></h1>
    
    <div class="controls"> 
        <?php echo "<?php\n" ; ?>
        echo CHtml::button('Create', array('class' => 'controls', 'id' => 'create-<?php echo $this->class2id($this->modelClass); ?>'));
        echo CHtml::button('Delete', array('class' => 'controls', 'id' => 'delete-<?php echo $this->class2id($this->modelClass); ?>'));
        <?php echo "?>\n";?>
    </div>
</div>

<div class="content">
    
<?php echo "<?php
// echo CHtml::link('Advanced Search','#',array('class'=>'search-button'));
// echo '<div class=\"search-form\" style=\"display:none\">';   
// \$this->renderPartial('_search',array(
//	'model'=>\$model,
// ));
// echo '</div><!-- search-form -->'; \n?>\n"; ?>


<?php echo "<?php"; ?> $this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'<?php echo $this->class2id($this->modelClass); ?>-grid',
	'dataProvider'=>$model->search(),
	'filter'=>$model,
        'cssFile' => Util::themeUrl() . '/css/gridview.css',
        'htmlOptions'=>array('class'=>'grid-view clear'),
        'afterAjaxUpdate'=>'js:function(id,data){$.bind_data()}',
	'columns'=>array(
        array(
            'class' => 'CCheckBoxColumn',
            'selectableRows' => 2,
            'id' => 'chk',
            'value' => $data->id,
        ),
<?php
$count=0;
foreach($this->tableSchema->columns as $column)
{
	if(++$count==7)
		echo "\t\t/*\n";
	echo "\t\t'".$column->name."',\n";
}
if($count>=7)
	echo "\t\t*/\n";
?>
		array(
                        'class'=>'CButtonColumn',
                        'viewButtonImageUrl' => false,
                        'updateButtonImageUrl' => false,
                        'deleteButtonImageUrl' => false,
                        'template' => '[{view}] [{update}]'
                      
		),
	),
)); ?>
</div>

<?php echo "<script  type=\"text/javascript\">\n";?>
    $(document).ready(function() {
        $. bind_data = function(){
            $(".grid-view a.view").click(function() {
                var url = $(this).attr('href') + '?ajax=true';
                $.fancybox.open({
                    href : url,
                    type : 'iframe',
                    afterClose: function(){
                        $.fn.yiiGridView.update('<?php echo $this->class2id($this->modelClass); ?>-grid')
                    }
                });
                return false;
            });
            
            $(".grid-view a.update").click(function() {
                var url = $(this).attr('href') + '?ajax=true';
                $.fancybox.open({
                    href : url,
                    type : 'iframe',
                    afterClose: function(){
                        $.fn.yiiGridView.update('<?php echo $this->class2id($this->modelClass); ?>-grid')
                    }
                });
                return false;
            });
        }
        
        // bind data
        $. bind_data();
        
        $("#create-<?php echo $this->class2id($this->modelClass); ?>").click(function() {
                $.fancybox.open({
                    href : '<?php echo "<?php echo \$this->createUrl('create', array('ajax' => true)) ?>" ?>',
                    type : 'iframe',
                    afterClose: function(){
                        $.fn.yiiGridView.update('<?php echo $this->class2id($this->modelClass); ?>-grid')
                    }
                });
                return false;
            });
        
        $('#delete-<?php echo $this->class2id($this->modelClass); ?>').live('click', function(){
            var count=0;
            var params = 'YII_CSRF_TOKEN=<?php echo('<?php echo '); ?> Yii::app()->request->csrfToken; <?php echo('?>'); ?>';
            $("input[name='chk[]']").each(function(){
            if($(this).is(":checked")){
                count++;     
                params += '&chk[]=' + $(this).val();
            }
            });
            if(count >= 1 ){
            if(!confirm("Delete selected records"))
                return false;
            }else{
                alert("No checkbox is selected");
                return false;
            } 
            
            $.ajax({
                    type: "POST",
                    url: '<?php echo "<?php echo \$this->createUrl('delete', array('ajax' => true)) ?>" ?>',
                    data: params,
                    success: function(data) {
                        $.fn.yiiGridView.update('<?php echo $this->class2id($this->modelClass); ?>-grid');
                    } //success
                });//ajax
                return false;
        });
    });
<?php echo '</script>'; ?>